/**
  ******************************************************************************
  * File Name          : ctrl_shl.c
  * Description        : Code for data process applications
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "tskcfg.h"
#include "bsp.h"
#include "spiflash.h"
#include "collector.h"
#include "ctrl_common.h"



void Ctrl_shl_mode_jt(int16_t temper)
{
  uint8_t ch;
  
  for(ch=0; ch<CTR_SHL_CHANNEL_ALLNUM; ch++)
  {
		if(temper <= Ctrl_ParaJT.sllotp)
    {
      sl_act[ch] = 0;
      Ctrl_StateSL.slst[ch] = 'l';
			continue;
    }
		
    sl_act[ch] = 2;
		if(CtrShl_GetActuator(ch) != 0)
		{
			Ctrl_StateSL.slst[ch] = 'R';
		}
		else
		{
			Ctrl_StateSL.slst[ch] = 'r';
		}
        
    if(temper >= Ctrl_ParaJT.slhitp)
    {
      sl_act[ch] = 1;
      Ctrl_StateSL.slst[ch] = 'J';
    }
    
    if(temper < (Ctrl_ParaJT.slhitp - Ctrl_ParaJT.slrttp))
    {
      sl_act[ch] = 0;
      Ctrl_StateSL.slst[ch] = 'j';
    }
  }
}

void Ctrl_shl_mode_dl(int16_t temper, uint32_t ticker)
{
  uint8_t ch;
  
  for(ch=0; ch<CTR_SHL_CHANNEL_ALLNUM; ch++)
  {
    if(Ctrl_ParaDL1.sltp[ch] == 0)
    {
      if((Ctrl_ParaSK.slen != 0) && (Ctrl_ParaSK.sltd[ch] != 0))
      {
				Ctr_TcTickers.shl_tc_recont = 1;
				
        if(ticker <= Ctrl_ParaSK.slkq)
        {
          sl_act[ch] = 1;
          Ctrl_StateSL.slst[ch] = 'S';
        }
        else if((ticker > Ctrl_ParaSK.slkq) && (ticker <= (Ctrl_ParaSK.slkq + Ctrl_ParaSK.slgb)))
        {
          sl_act[ch] = 0;
          Ctrl_StateSL.slst[ch] = 's';
        }
				else
        {
          sl_act[ch] = 0;
          Ctrl_StateSL.slst[ch] = 's';
        }
      }
      else
      {
        sl_act[ch] = 0;
        Ctrl_StateSL.slst[ch] = 'd';
      }
    }
    else /* Ctrl_ParaDL.fntp[ch] != 0 */
    {
      sl_act[ch] = 2;
			if(CtrShl_GetActuator(ch) != 0)
			{
				Ctrl_StateSL.slst[ch] = 'E';
			}
			else
			{
				Ctrl_StateSL.slst[ch] = 'e';
			}
      
      if(temper >= Ctrl_ParaDL1.sltp[ch])
      {
        sl_act[ch] = 1;
        Ctrl_StateSL.slst[ch] = 'D';
      }
      
      if(temper < (Ctrl_ParaDL1.sltp[ch] - Ctrl_ParaDL2.slrttp))
      {
        sl_act[ch] = 0;
        Ctrl_StateSL.slst[ch] = 'd';
      }
    }
  }
}

void Ctrl_shl_mode_sk(int16_t temper, uint32_t ticker)
{
	uint8_t ch;
  
  for(ch=0; ch<CTR_SHL_CHANNEL_ALLNUM; ch++)
  {
		if(Ctrl_ParaMD.slmdsken == 0)
		{
			sl_act[ch] = 0;
			Ctrl_StateSL.slst[ch] = 'q';
			continue;
		}
		
    if((Ctrl_ParaSK.slen != 0) && (Ctrl_ParaSK.sltd[ch] != 0))
    {
			Ctr_TcTickers.shl_tc_recont = 1;
			
      if(ticker <= Ctrl_ParaSK.slkq)
      {
        sl_act[ch] = 1;
        Ctrl_StateSL.slst[ch] = 'T';
      }
      else if((ticker > Ctrl_ParaSK.slkq) && (ticker <= (Ctrl_ParaSK.slkq + Ctrl_ParaSK.slgb)))
      {
        sl_act[ch] = 0;
        Ctrl_StateSL.slst[ch] = 't';
      }
			else
      {
        sl_act[ch] = 0;
        Ctrl_StateSL.slst[ch] = 't';
      }
    }
    else
    {
      sl_act[ch] = 0;
      Ctrl_StateSL.slst[ch] = 'p';
    }
  }
}

void Ctrl_shl_mode_zn(int16_t temper, uint32_t ticker)
{
  uint8_t ch;
  
  for(ch=0; ch<CTR_SHL_CHANNEL_ALLNUM; ch++)
  {
    if(temper <= Ctrl_ParaJT.sllotp)
    {
      sl_act[ch] = 0;
      Ctrl_StateSL.slst[ch] = 'w';
      continue;
    }
    
    sl_act[ch] = 2;
		if(CtrShl_GetActuator(ch) != 0)
		{
			Ctrl_StateSL.slst[ch] = 'X';
		}
		else
		{
			Ctrl_StateSL.slst[ch] = 'x';
		}
		
    if(temper >= Ctrl_ParaJT.slhitp)
    {
      sl_act[ch] = 1;
      Ctrl_StateSL.slst[ch] = 'Z';
    }
    
    if(temper < (Ctrl_ParaJT.slhitp - Ctrl_ParaJT.slrttp))
    {
      sl_act[ch] = 0;
      Ctrl_StateSL.slst[ch] = 'z';
    }
    
		if((Ctrl_ParaSK.slen != 0) && (Ctrl_ParaSK.sltd[ch] != 0) && (temper <= Ctrl_ParaJT.slhitp) && (temper > Ctrl_ParaJT.sllotp))
    {
			Ctr_TcTickers.shl_tc_recont = 1;
			
      if(ticker <= Ctrl_ParaSK.slkq)
      {
        sl_act[ch] = 1;
        Ctrl_StateSL.slst[ch] = 'Y';
      }
      else if((ticker > Ctrl_ParaSK.slkq) && (ticker <= (Ctrl_ParaSK.slkq + Ctrl_ParaSK.slgb)))
      {
        sl_act[ch] = 0;
        Ctrl_StateSL.slst[ch] = 'y';
      }
			else
      {
        sl_act[ch] = 0;
        Ctrl_StateSL.slst[ch] = 'y';
      }
    }
  }
}

void CtrShl_TimeCtrlISR(void)
{
	if(Ctr_TcTickers.shl_tc_ticker_en != 0)
	{
		if(Ctr_TcTickers.shl_tc_ticker++ > (Ctrl_ParaSK.slkq + Ctrl_ParaSK.slgb))
		{
			Ctr_TcTickers.shl_tc_ticker = 0;
		}
	}
	else
	{
		Ctr_TcTickers.shl_tc_ticker = 0;
	}
}
